documentWidth = window.screen.availWidth //可用区域
gridContainerWidth = 0.92*documentWidth //棋盘宽度
cellSideLength = 0.18*documentWidth //小方格宽度
cellSpace = 0.04*documentWidth  //小方格间距

function getPosTop(i, j) {
    return cellSpace + i * (cellSpace+cellSideLength);
}

function getPosLeft(i, j) {
    return cellSpace + j * (cellSpace+cellSideLength);
}

function getNumberBackgroundColor(number) {
    switch (number) {
        case 2: return "#eee4da"; break;
        case 4: return "#ede0c8"; break;
        case 8: return "#f2b179"; break;
        case 16: return "#f59563"; break;
        case 32: return "#f67e5f"; break;
        case 64: return "#f65e3b"; break;
        case 128: return "#edcf72"; break;
        case 256: return "#edcc61"; break;
        case 512: return "#9c0"; break;
        case 1024: return "#33b5e5"; break;
        case 2048: return "#09c"; break;
        case 4096: return "#a6c"; break;
        case 8192: return "#93c"; break;
    }
    return "black"
}

function getNumberColor(number) {
    if (number <= 4) {
        return "#776e65"
    }
    return "white"
}
//是否能移动
function nospace(board) {
    for (let i = 0; i < 4; i++) {
        for (let j = 0; j < 4; j++) {
            if (board[i][j] == 0) {
                return false
            }
        }
    }
    return true
}
//是否能向左移动
function canMoveLeft(board){
    for(let i=0; i<4;i++){
        for(let j=1;j<4;j++){
            if(board[i][j]!=0){
                if(board[i][j-1]==0||board[i][j-1]==board[i][j]){
                    return true
                }
            }
        }
    }
    return false
}
//能否向右移动
function canMoveRight(board){
    for(let i=0;i<4;i++){
        for(let j=2;j>=0;j--){
            if(board[i][j]!=0){
                if(board[i][j+1]==0||board[i][j+1]==board[i][j]){
                    return true
                }
            }
        }
    }
    return false
}
//能否向上移动
function canMoveUp(board){
    for(let j=0;j<4;j++){
        for(let i=1;i<4;i++){
            if(board[i][j]!=0){
                if(board[i-1][j]==0||board[i-1][j]==board[i][j]){
                    return true
                }
            }
        }
    }
    return false
}
//能否向下移动
function canMoveDown(board){
    for(let j=0;j<4;j++){
        for(let i=2;i>=0;i--){
            if(board[i][j]!=0){
                if(board[i+1][j]==0||board[i+1][j]==board[i][j]){
                    return true
                }
            }
        }
    }
    return false
}
//障碍判断
function noBlockHorizontal(row,col1,col2,board){
    for(let i=col1+1;i<col2;i++){
        if(board[row][i]!=0){
            return false
        }
    }
    return true
}
function noBlockVertical(col,row1,row2,board){
    for(let i=row1+1;i<row2;i++){
        if(board[i][col]!=0){
            return false
        }
    }
    return true
}
//不能移动判断
function nomove(board){
    if(canMoveLeft(board)||canMoveRight(board)||canMoveUp(board)||canMoveDown(board)){
        return false
    }
    return true
}